package com.gurun.camera.account.activity;


import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.danale.sdk.platform.constant.user.VerifyType;
import com.danale.sdk.platform.entity.countrycode.CountryCode;
import com.gurun.camera.account.presenter.InputVerifycodePresenterImpl;
import com.gurun.camera.account.presenter.LoginPresenterImpl;
import com.gurun.camera.account.presenter.SetPasswordPresenterImpl;
import com.gurun.camera.account.presenter.VerifyAccountPresenterImpl;
import com.gurun.camera.account.view.IAccountView;
import com.gurun.camera.account.view.IInputVerifycodeView;
import com.gurun.camera.account.view.ILoginView;
import com.gurun.camera.account.view.ISetPasswordView;
import com.gurun.camera.model.event.CameraAccountRegistResultEvent;
import com.gurun.camera.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class AccountUtil implements ILoginView, IAccountView, IInputVerifycodeView, ISetPasswordView {

    private static final String DEFAULT_PASS = "QAZxsw!@#$";
    private boolean mIsLogin = false;//是否已经登录成功了
    private static final String SP_KEY_USER_PHONE_NUM = "key_user_phone_num";//一定得跟App.java中的保持一致

    private static AccountUtil mInstance;
    private String mPhoneNum = "";

    private AccountUtil() {
    }

    private static Context mContext;
    private Context mAppContext;

    public static AccountUtil getInstance(Context context) {
        if (context != null){
            mContext = context;
        }
        if (mInstance == null) {
            synchronized (AccountUtil.class) {
                if (mInstance == null) {
                    mInstance = new AccountUtil();
                }
            }
        }
        return mInstance;
    }

    public Context getAppContext() {
        return mAppContext;
    }

    public void setAppContext(Context mAppContext) {
        mAppContext = mAppContext;
    }

    private String getPhoneNum() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        mPhoneNum = sp.getString(SP_KEY_USER_PHONE_NUM,"");
        return mPhoneNum;
    }

    //===================login start=================================
    private static LoginPresenterImpl loginPre;

    private void initLoginPresenter() {
        loginPre = new LoginPresenterImpl(this);
    }

    public void login() {
        if (loginPre == null) {
            initLoginPresenter();
        }
        loginPre.login(getPhoneNum(), DEFAULT_PASS);

    }

    @Override
    public void showCountryCode() {

    }

    @Override
    public void showCountryFlag() {

    }

    @Override
    public void hideCountry() {

    }

    @Override
    public void notifyloginResult(String result) {
        if ("SUCCESS".equals(result)) {
            mIsLogin = true;
            EventBus.getDefault().post(new CameraAccountRegistResultEvent(true));
        } else {
            EventBus.getDefault().post(new CameraAccountRegistResultEvent(false));
        }
    }

    @Override
    public void hideIllegalLayout() {

    }

    @Override
    public void noyifyCurrentCountryCode(CountryCode code) {
        countryCode = code;
    }

    @Override
    public void showloading() {

    }

    @Override
    public void hideloading() {

    }

    public boolean isLoginSuccessfully() {
        return mIsLogin;
    }
    //===================login end=================================


    //===================send verify code start=================================

    private VerifyAccountPresenterImpl mVerifyAccPre;
    private CountryCode countryCode = new CountryCode();

    private void initRegistPresenter() {
        mVerifyAccPre = new VerifyAccountPresenterImpl(this);
    }

    public void getCountryCode() {
        if (mVerifyAccPre == null) {
            initRegistPresenter();
        }
        mVerifyAccPre.getCurrentCountryCode();
    }

    public void getVerifyCode() {
        if (mVerifyAccPre == null) {
            initRegistPresenter();
        }
        mVerifyAccPre.verifyAccountLegal(getPhoneNum(), VerifyAccountPresenterImpl.REGIST_SEND_CODE, countryCode.getPhoneCode());
    }

    @Override
    public void notifyIllegalAccount(VerifyAccountPresenterImpl.IllegalType type) {

    }

    @Override
    public void showSendVerifycodeProgressBar() {

    }

    @Override
    public void hideSendVerifycodeProgressBar() {

    }

    @Override
    public void sendVerifycodeResult(String result) {
        if ("SUCCESS".equals(result)) {
            ToastUtil.showShortToast(mContext,"验证码发送成功");
        } else {
            ToastUtil.showShortToast(mContext,"验证码发送失败");
        }
    }
    //===================send verify code end=================================


    //===================check verify code start=================================

    private InputVerifycodePresenterImpl inputVcPre;
    private String verifyCode;

    private void initCheckVerifyCodePresenter() {
        inputVcPre = new InputVerifycodePresenterImpl(this);
    }

    public void checkVerifyCode(String code) {
        if (inputVcPre == null) {
            initCheckVerifyCodePresenter();
        }
        verifyCode = code;
        inputVcPre.countDownResend();
        inputVcPre.checkVerifyCode(getPhoneNum(), " ", VerifyType.USER_REGIST_VERIFY, code);
    }

    public void countDownResend() {
        if (inputVcPre != null) {
            inputVcPre.countDownResend();
        }
    }


    @Override
    public void notifyResendVerifyCodeState(int resendType) {

    }

    @Override
    public void notifyCheckVerifyCodeResult(String result) {
        if ("SUCCESS".equals(result)) {
            regist(verifyCode);
        } else {
            ToastUtil.showShortToast(mContext,"验证码错误");
            EventBus.getDefault().post(new CameraAccountRegistResultEvent(false));
        }
    }
    //===================check verify code end=================================


    //===================regist start=================================

    private SetPasswordPresenterImpl setPwdPre;
    private static int VERIFY_TYPE = -1;

    private void initSetPassPresenter() {
        setPwdPre = new SetPasswordPresenterImpl(this);
    }

    private void regist(String verifycode) {
        if (setPwdPre == null) {
            initSetPassPresenter();
        }
        setPwdPre.createAccount(getPhoneNum(), DEFAULT_PASS, verifycode, " ", VERIFY_TYPE);
    }

    @Override
    public void notifyCreateAccountResult(String result) {
        if ("SUCCESS".equals(result)) {
            login();
        } else {
            ToastUtil.showShortToast(mContext,"与服务器连接失败");
            EventBus.getDefault().post(new CameraAccountRegistResultEvent(false));
        }
    }

    //===================regist end=================================


}
